﻿using System.Collections.Generic;
using JetBrains.Annotations;

namespace com.susi.util.toolkit
{
    //[NotNull]
    public interface IPropertySource
    {
        bool ContainsFloKey(string key);
        // bool ContainsKey(string key);
        bool ContainsStrKey(string key);
        bool TryGet(string key, out float value);
        bool TryGet(string key, out string value);
        /// <summary>
        /// 不判断直接获取值
        /// <exception cref="KeyNotFoundException"></exception>
        /// </summary>
        float GetFlo(string key);
        /// <summary>
        /// 不判断直接获取值
        /// <exception cref="KeyNotFoundException"></exception>
        /// </summary>
        [CanBeNull]
        string GetStr(string key);
    }
    
    public static class IPropertySourceExtension{
        public static bool ContainsKey(this IPropertySource source, string key)
        {
            return source.ContainsFloKey(key) || source.ContainsStrKey(key);
        }

        public static float GetFloOrDefault(this IPropertySource sou, string key)
        {
            sou.TryGet(key, out float value);
            return value;
        }
        
        public static string GetStrOrDefault(this IPropertySource sou, string key)
        {
            sou.TryGet(key, out string value);
            return value;
        }
    }

    public class EmptyPropertySource:IPropertySource
    {
        private static EmptyPropertySource _Ins;
        public static EmptyPropertySource Instance
        {
            get
            {
                if (_Ins == null) _Ins = new EmptyPropertySource();
                return _Ins;
            }
        }

        public IEnumerable<string> Keys
        {
            get
            {
                yield break;
            }
        }

        public bool ContainsFloKey(string key)
        {
            return false;
        }

        public bool ContainsKey(string key)
        {
            return false;
        }

        public bool ContainsStrKey(string key)
        {
            return false;
        }

        public float GetFlo(string key)
        {
            return default;
        }

        public string GetStr(string key)
        {
            return default;
        }

        public bool TryGet(string key, out float value)
        {
            value = default;
            return false;
        }

        public bool TryGet(string key, out string value)
        {
            value = default;
            return false;
        }
    }

    public static class IPropertySourceExtensions
    {
        public static float GetFloValueOrDefault(this IPropertySource propertySource,string key)
        {
            if(propertySource.TryGet(key,out float res))
            {
                return res;
            }
            return default;
        }
        public static string GetStrValueOrDefault(this IPropertySource propertySource, string key)
        {
            if (propertySource.TryGet(key, out string res))
            {
                return res;
            }
            return default;
        }
    }
}
